<template>
  <div class="app-container">
    <el-form :model="form" ref="form" label-width="100px" v-loading="formLoading" :rules="rules"> 
      <el-form-item label="学科：" >
        <el-select v-model="form.subjectId" placeholder="学科">
         <el-option label="数学" value="1" />
         <el-option label="物理" value="2" />
        </el-select>
      </el-form-item> 
      <el-form-item label="试卷名称："  prop="name" required>
        <el-input v-model="form.name"/>
      </el-form-item>
      <el-form-item label="题目数量：" prop="maxnum" required>
        <el-input-number v-model="form.maxnum" @change="handleChange" :min="1" :max="20" label="数量"></el-input-number>
      </el-form-item>
      <el-form-item label="试卷考查："  prop="tag" required>
        <el-input v-model="form.tag" type="textarea"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { addPaper} from '@/api/examApi.js'
export default {
  data () {
    return {
      form: {
        id: null,
        subjectId: '1',
        name: '',
        maxnum: 10,
        tag: ''
      },
      formLoading: false,
      rules: {
        subjectId: [
          { required: true, message: '请选择学科', trigger: 'change' }
        ],
        name: [
          { required: true, message: '请输入试卷名称', trigger: 'blur' }
        ],
        maxnum: [
          { required: true, message: '请输入最大题目数量', trigger: 'blur' }
        ],
        tag: [
          { required: true, message: '请输入试卷考点说明', trigger: 'blur' }
        ]
      }
     
    }
  },
  created () {
    let id = this.$route.query.id
    let _this = this
  },
  methods: {
    submitForm () {
      let _this = this
      this.$refs.form.validate((valid) => {
        if (valid) {
          //this.formLoading = true
          var jsData = {"subject":this.form.subjectId,"maxnum":this.form.maxnum,"title":this.form.name,"tag":this.form.tag,"auth":_this.username}
          //alert(JSON.stringify(jsData))
          addPaper(jsData).then(re => {
            if (re.code === 200) {
               _this.$message.success(re.message)
               _this.resetForm()
               } else {
                _this.$message.error(re.message)
              }})
        } else {
          this.formLoading = false
        }
      })
    },
    resetForm () {
      //let lastId = this.form.id
      this.$refs['form'].resetFields()
      this.form = {
        //id: null,
        subjectId: null,
        name: '',
        maxnum: 10,
        tag: '',
      }
      //this.form.id = lastId
    },

  },
   computed: {
    ...mapGetters({username:"name", score:"score",rank:"rank" }),
  },
}
</script>

<style lang="scss">
  .exampaper-item-box {
    .q-title {
      margin: 0px 5px 0px 5px;
    }
    .q-item-content {
    }
  }
</style>